package com.rykj.qxj.server.common.utils.generator;

import java.lang.reflect.Field;
/**
 * 
 * @Description: mapper.xml生成类   注意：所有实体主键假定为id 
 * @date:   2019年3月13日 上午9:49:04  
 * @author: wangshunyi
 */
public class MapperUtils {
	public static boolean generateMapper(Class clazz){
		StringBuilder sb = new StringBuilder();
		sb.append("<!DOCTYPE mapper\n")
			.append("\tPUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\"\n")
			.append("\t\"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\n")
			.append("\n");
		sb.append("<mapper namespace=\"").append(clazz.getSimpleName()).append("\">\n");
		sb.append("\t<resultMap id=\"resultMap\" type=\"").append(clazz.getName()).append("\">\n");
		sb.append("\t\t<id column=\"id\" property=\"id\"/>\n");
		Field[] fields = clazz.getDeclaredFields();
		StringBuilder associations = new StringBuilder();
		/**生成resultMap中result*/
		for (Field field : fields) {
			String fieldName = field.getName();
			if(field.getGenericType().getTypeName().indexOf(".model.") != -1){ //外键
				associations.append("\t\t<association property=\"").append(fieldName)
					.append("\" javaType=\"").append(field.getGenericType().getTypeName()).append("\">\n");
				associations.append("\t\t\t<id column=\"").append(BaseGeneratorUtils.camel2Underline(fieldName)+"_id").append("\" property=\"id\"/>\n");
				associations.append("\t\t</association>\n");
			}else if(!"id".equals(fieldName)){
				sb.append("\t\t<result column=\"").append(BaseGeneratorUtils.camel2Underline(fieldName))
					.append("\" property=\"").append(fieldName).append("\"/>\n");
			}
		}
		sb.append(associations);
		sb.append("\t</resultMap>\n\n");
		/**生成 sql columns*/
		sb.append("\t<sql id=\"columns\">\n\t\t");
		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			String columnName = BaseGeneratorUtils.getColumn(field);
			sb.append("`").append(columnName).append("`");
			if(i < fields.length-1){
				sb.append(",");
			}
		}
		sb.append("\n\t</sql>\n\n");
		/**生成 sql properties*/
		sb.append("\t<sql id=\"properties\">\n\t\t");
		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			String columnName = BaseGeneratorUtils.getFieldName(field);
			sb.append("#{").append(columnName).append("}");
			if(i < fields.length-1){
				sb.append(",");
			}
		}
		sb.append("\n\t</sql>\n\n");
		
		/**生成insert*/
		sb.append("\t<insert id=\"insert\">\n");
		sb.append("\t\tinsert into ").append(BaseGeneratorUtils.camel2Underline(clazz.getSimpleName()))
			.append(" (<include refid=\"columns\"></include>)\n");
		sb.append("\t\tvalues (<include refid=\"properties\"></include>)\n");
		sb.append("\t</insert>\n\n");
		
		/**生成updateByPrimaryKey*/
		sb.append("\t<update id=\"updateByPrimaryKey\">\n");
		sb.append("\t\tupdate ").append(BaseGeneratorUtils.camel2Underline(clazz.getSimpleName())).append("\n");
		sb.append("\t\t<set>\n");
		for (Field field : fields) {
			sb.append("\t\t\t").append(BaseGeneratorUtils.getColumn(field)).append(" = ")
				.append("#{").append(BaseGeneratorUtils.getFieldName(field)).append("},\n");
		}
		sb.append("\t\t</set>\n");
		sb.append("\t\twhere id = #{id}\n");
		sb.append("\t</update>\n\n");
		
		/**生成updateByPrimaryKeySelective*/
		sb.append("\t<update id=\"updateByPrimaryKeySelective\">\n");
		sb.append("\t\tupdate ").append(BaseGeneratorUtils.camel2Underline(clazz.getSimpleName())).append("\n");
		sb.append("\t\t<set>\n");
		for (Field field : fields) {
			sb.append("\t\t\t<if test=\"");
			if(field.getGenericType().getTypeName().indexOf(".model.") != -1){
				sb.append(field.getName()).append(" != null and ").append(field.getName()).append(".id != null");
			}else{
				sb.append(field.getName()).append(" != null");
			}
			sb.append("\">\n");
			
			sb.append("\t\t\t\t").append(BaseGeneratorUtils.getColumn(field)).append(" = ")
				.append("#{").append(BaseGeneratorUtils.getFieldName(field)).append("},\n");
			sb.append("\t\t\t</if>\n");
		}
		sb.append("\t\t</set>\n");
		sb.append("\t\twhere id = #{id}\n");
		sb.append("\t</update>\n\n");
		
		/**生成deleteByPrimaryKey*/
		sb.append("\t<delete id=\"deleteByPrimaryKey\">\n");
		sb.append("\t\tdelete from ").append(BaseGeneratorUtils.camel2Underline(clazz.getSimpleName())).append(" where id = #{id}\n");
		sb.append("\t</delete>\n");
		
		/**生成selectByPrimaryKey*/
		sb.append("\t<select id=\"selectByPrimaryKey\" resultMap=\"resultMap\">\n");
		sb.append("\t\tselect * from ").append(BaseGeneratorUtils.camel2Underline(clazz.getSimpleName())).append(" where id = #{id}\n");
		sb.append("\t</select>\n\n");
		
		/**生成selectByExample*/
		sb.append("\t<select id=\"selectByExample\" resultMap=\"resultMap\">\n");
		sb.append("\t\tselect * from ").append(BaseGeneratorUtils.camel2Underline(clazz.getSimpleName())).append("\n");
		sb.append("\t\t<where>\n");
		for (Field field : fields) {
			sb.append("\t\t\t<if test=\"");
			if(field.getGenericType().getTypeName().indexOf(".model.") != -1){
				sb.append(field.getName()).append(" != null and ").append(field.getName()).append(".id != null");
			}else{
				sb.append(field.getName()).append(" != null");
			}
			sb.append("\">\n");
			
			sb.append("\t\t\t\tand ").append(BaseGeneratorUtils.getColumn(field)).append(" = ")
				.append("#{").append(BaseGeneratorUtils.getFieldName(field)).append("}\n");
			sb.append("\t\t\t</if>\n");
		}
		sb.append("\t\t</where>\n");
		sb.append("\t</select>\n\n");
		
		sb.append("</mapper>");
		
		//保存至文件中
		String folderPath = clazz.getResource("").toString().replace("/target/classes", "/src/main/java") + "mapper";
		String filePath = folderPath+"/"+clazz.getSimpleName()+"Mapper.xml";
		return BaseGeneratorUtils.writeIntoFile(filePath, sb.toString());
	}
	
}
